package com.maven.front.controller.frontSubject;

import com.maven.front.aop.MyLog;
import com.maven.front.bean.*;
import com.maven.front.dto.EduCommentDto;
import com.maven.front.dto.EduCourseDto;
import com.maven.front.service.frontSubject.SubjectService;
import com.maven.front.service.manageCourse.EduCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @program: web-front
 * @description: 前台课程
 * @author: WangYiLong
 * @create: 2020-08-03 08:52
 **/
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("user")
public class FrontCourseController {

    @Resource
    private EduCourseService eduCourseService;

    @Resource
    private SubjectService subjectService;
    /**
     *  根据课程ID返回课程所有信息及课程下所有节点，用于前端course-video页面初始化
     * @param id 课程ID
     * @return
     */
    @MyLog(value = "课程详细信息页面显示")
    @GetMapping("/courseVideo/init")
    public Result getCourseById(Integer id) {
        log.info("**************"+id);
        //课程信息
        EduCourseDto dto = eduCourseService.getById(id);
        //课程节点信息
        List<EduCourseKpoint> list = subjectService.getKpointListByCourseId(id);
        //教师信息
        List<EduTeacher> teachers = subjectService.getTeachersByCourseId(id);
        //评论信息
        List<EduCommentDto> comments = subjectService.getCommentsByCourseId(id);
        Collections.sort(list);
        if(ObjectUtils.isEmpty(dto)||CollectionUtils.isEmpty(list)||CollectionUtils.isEmpty(teachers)){
            return new Result(222,"查询失败");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("course",dto);
        map.put("kpoints",list);
        map.put("teachers",teachers);
        map.put("comments",comments);
        return new Result(200,"成功",map);
    }

    @MyLog(value = "课程页面显示")
    @GetMapping("subject")
    public Result getSubjects(EduCourse eduCourse){
        List<SysSubject> subjects = subjectService.getSubjects();
        List<EduTeacher> teachers = subjectService.getTeachers();
        List<EduCourse> courses = subjectService.getEduCourses(eduCourse);
        Map<String,Object> map = new HashMap<>();
        map.put("listSysSubject",subjects);
        map.put("listEduCourseDto",courses);
        map.put("teachers",teachers);
        return new Result(200,"成功",map);
    }

    /**
     * 课程排序
     * @return
     */
    @MyLog(value = "精品课程、最新课程、全部课程查询")
    @GetMapping("subject/sort")
    public Result sortBbyViewCount(int type){
        List<EduCourse> courses = subjectService.getEduCourses(null);
        if(type==1){
            Collections.sort(courses, new Comparator<EduCourse>() {
                @Override
                public int compare(EduCourse o1, EduCourse o2) {
                    return o1.getPageViewcount()>o2.getPageViewcount()?-1:o1.getPageViewcount()<o2.getPageViewcount()?1:0;
                }
            });
        }else if(type==2){
            Collections.sort(courses, new Comparator<EduCourse>() {
                @Override
                public int compare(EduCourse o1, EduCourse o2) {
                    return o2.getAddTime().compareTo(o1.getAddTime());
                }
            });
        }else{
            Collections.sort(courses, new Comparator<EduCourse>() {
                @Override
                public int compare(EduCourse o1, EduCourse o2) {
                    return o2.getSourcePrice().compareTo(o1.getSourcePrice());
                }
            });
        }

        return new Result(200,"成功",courses);
    }


}
